u "$data_clean_failure/riskdata_analysis_2004_matched.dta",clear

* ------------------------------------------ *
*          	DID on subprime lending   	     *
* ------------------------------------------ *
** relabel to fit the table
la var pct_subprime "All"  // = subprime/originations
la var pct_subprime_conv "Homogeneous" // subprime_conv/origination_conv

la var pct_subprime_first  "first" // need first_originations
la var pct_subprime_second "second"  // need second_originations

la var pct_subprime_homequity "home equity"
la var pct_subprime_refi "refinance"

** robustness check: conventional subprime loans & other fixed effects
quietly{
	eststo clear
	local reg_results ""
	foreach fe in state id{
		foreach depvar of global Subprime { 			
			eststo `depvar'_`fe': reghdfe `depvar' bank_pre bank $Institution_04 $Borrower $State [fw=N], a(`fe'_num year) vce(cluster id_num)
			estadd ysumm
			local reg_results "`reg_results' `depvar'_`fe'"
		}
	}
	estfe `reg_results', labels(state_num "State FE" id_num "Institutional FE" year "Year FE")
	noisily:esttab `reg_results' using "$outputs_failure/regtables_matched/subprime_conv.tex" , ///
					indicate("Institution Characteristics = $Institution_04" ///
									 "Borrower Characteristics = $Borrower" ///
									 "State Controls = $State" `r(indicate_fe)', labels($\times$ " ")) ///
					replace lab compress gap booktab alignment(c) ///
					stats(N r2 ymean, fmt(%9.0f %9.3f %9.3f) labels("$\mathnormal{N}$" "$\mathnormal{R^2}$" "Outcome mean")) ///
					cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///	
					collabels(none) ///
					nobaselevels noomitted  nocons ///
					mgroups("Subprime Share (\%)" "Subprime Share (\%)", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))			
	estfe `reg_results', restore
}




* ------------------------------------------ *
*              DID on performance     		   *
* ------------------------------------------ *
la var delqRatio "all"
la var netChargeOffRatio "all"
la var delqRatio_mort "mortgage"
la var netChargeOffRatio_mort "mortgage"

la var delqRatio_fstmort "delinquency"
la var netChargeOffRatio_fstmort "net charge-off"

la var delqRatio_secmort "delinquency"
la var netChargeOffRatio_secmort "net charge-off"


** default, state FE
quietly{
	eststo clear
	foreach depvar of global Performance { 			
		eststo `depvar': reghdfe `depvar' bank_crisis bank $Institution_04 $Borrower $State [fw=N], a(state_num year) vce(cluster id_num)
		estadd ysumm
	}
	estfe $Performance, labels(state_num "State FE" id_num "Institutional FE" year "Year FE")
	noisily:esttab $Performance using "$outputs_failure/regtables_matched/InstPerformance_state.tex", ///
					indicate("Institution Characteristics = $Institution_04" ///
									  "Borrower Characteristics = $Borrower" ///
									 "State Controls = $State" `r(indicate_fe)', labels($\times$ " ")) ///
					replace lab compress gap booktab alignment(c) ///
					stats(N r2 ymean, fmt(%9.0f %9.3f %9.3f) labels("$\mathnormal{N}$" "$\mathnormal{R^2}$" "Outcome mean")) ///
					cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///	
					collabels(none) ///
					nobaselevels noomitted  nocons ///
					mgroups("Failure" "Delinquency Ratio (\%)" "Net Charge-off Ratio (\%)", pattern(1 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	estfe $Performance, restore
}



* ------------------------------------------ *
*     Event study on subprime loan    *
* ------------------------------------------ *
** use 2010 as the base
quietly{
	foreach depvar of global Subprime {
		preserve
		reghdfe `depvar' bank_04-bank_09 bank_11-bank_17 bank $Institution_04 $Borrower $State [fw=N], a(state_num year) vce(cluster id_num)   
		** make a graph
		mat T = r(table) 
		mat list T 
		mat beta = T[1,1..13]
		mat se = T[2,1..13]
		mat se_u = T[5,1..13]
		mat se_l = T[6,1..13]
		mat beta = [beta[1,1..6],0,beta[1,7..13]]
		mat se = [se[1,1..6],0,beta[1,7..13]]
		mat se_u = [se_u[1,1..6], 0, se_u[1,7..13]]
		mat se_l = [se_l[1,1..6], 0, se_l[1,7..13]]
		mat R = [beta\se\se_u\se_l] 
		mat R = R' 
		svmat R // generate variables
		keep R1 R2 R3 R4   
		drop if R1==.  // make the propoer length
		gen n = _n 
		
		egen l = fill(-9 -8 -7) 
		twoway (scatter R1 n, msymbol(diamond) mfcolor (gs0) mlcolor(gs0) connect(l) lwidth(medthin) lpattern(solid) lcolor(gs0)) ///
			(line R4 n, lpattern(dash) lcolor(gs8)) ///
			(line R3 n, lpattern(dash) lcolor(gs8)), ///
			yline(0, lcolor(gs8)) ///
			xline(4, lcolor(red) lpattern(solid)) ///
			xline(6, lcolor(red) lpattern(solid)) ///
			xlabel(1 "04" 2 "05" 3 "06" 4 "07" 5 "08" 6 "09" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14" 12 "15" 13 "16" 14 "17", nogrid glcolor(gs14)) ///
			ylabel(, angle(h) nogrid) xtitle("Year") ytitle("Coefficients by year", size(small)) ///
			legend(label(1 "Point Estimates") label(2 "95% Upper bound") label(3 "95% lower bound") size(small)) ///
			graphregion(fcolor(white))
	
		noisily: graph export "$outputs_failure/graphs_matched/event_study_`depvar'.png", replace
		restore
	}
}

/* remarks:
	When doing i.post#c.($Institution_04). Coefficients after 2007 become very large. It's thus better not to do this way 
*/



* ------------------------------------------ *
*   		Event study on approval  	   *
* ------------------------------------------ *
** use 2010 as the base
gen log_subprime = log(subprime)
la var log_subprime "log subprime origination"

quietly{
	foreach depvar of global Approval {
		preserve
		reghdfe `depvar' bank_04-bank_09 bank_11-bank_17 bank $Institution_04 $Borrower $State [fw=N], a(state_num year) vce(cluster id_num)   
		** make a graph
		mat T = r(table) 
		mat list T 
		mat beta = T[1,1..13]
		mat se = T[2,1..13]
		mat se_u = T[5,1..13]
		mat se_l = T[6,1..13]
		mat beta = [beta[1,1..6],0,beta[1,7..13]]
		mat se = [se[1,1..6],0,beta[1,7..13]]
		mat se_u = [se_u[1,1..6], 0, se_u[1,7..13]]
		mat se_l = [se_l[1,1..6], 0, se_l[1,7..13]]
		mat R = [beta\se\se_u\se_l] 
		mat R = R' 
		svmat R // generate variables
		keep R1 R2 R3 R4   
		drop if R1==.  // make the propoer length
		gen n = _n 
		
		egen l = fill(-9 -8 -7) 
		twoway (scatter R1 n, msymbol(diamond) mfcolor (gs0) mlcolor(gs0) connect(l) lwidth(medthin) lpattern(solid) lcolor(gs0)) ///
			(line R4 n, lpattern(dash) lcolor(gs8)) ///
			(line R3 n, lpattern(dash) lcolor(gs8)), ///
			yline(0, lcolor(gs8)) ///
			xline(4, lcolor(red) lpattern(solid)) ///
			xline(6, lcolor(red) lpattern(solid)) ///
			xlabel(1 "04" 2 "05" 3 "06" 4 "07" 5 "08" 6 "09" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14" 12 "15" 13 "16" 14 "17", nogrid glcolor(gs14)) ///
			ylabel(, angle(h) nogrid) xtitle("Year") ytitle("Coefficients by year", size(small)) ///
			legend(label(1 "Point estimates") label(2 "95% Upper bound") label(3 "95% lower bound") size(small)) ///
			graphregion(fcolor(white))
	
		noisily: graph export "$outputs_failure/graphs_matched/event_study_`depvar'.png", replace
		restore
	}
}



* ------------------------------------------ *
*      Event study on performance 	   *
* ------------------------------------------ *
quietly{
	foreach depvar of global Performance {
		preserve
		reghdfe `depvar' bank_04-bank_05 bank_07-bank_17 bank $Institution_04 $Borrower $State [fw=N], a(state_num year) vce(cluster id_num)   
		
		** make a graph
		mat T = r(table) 
		mat list T 
		mat beta = T[1,1..13]
		mat se = T[2,1..13]
		mat se_u = T[5,1..13]
		mat se_l = T[6,1..13]
		mat beta = [beta[1,1..2], 0, beta[1,3..13]]
		mat se = [se[1,1..2], 0, se[1,3..13]]
		mat se_u = [se_u[1,1..2], 0, se_u[1,3..13]]
		mat se_l = [se_l[1,1..2], 0, se_l[1,3..13]]
		mat R = [beta\se\se_u\se_l] 
		mat R = R' 
		svmat R // generate variables
		keep R1 R2 R3 R4   
		drop if R1==.  // make the propoer length
		gen n = _n 
		
		egen l = fill(-9 -8 -7) 
		twoway (scatter R1 n, msymbol(diamond) mfcolor (gs0) mlcolor(gs0) connect(l) lwidth(medthin) lpattern(solid) lcolor(gs0)) ///
			(line R4 n, lpattern(dash) lcolor(gs8)) ///
			(line R3 n, lpattern(dash) lcolor(gs8)), ///
			yline(0, lcolor(gs8))  ///
			xline(4, lcolor(red) lpattern(solid)) ///
			xline(6, lcolor(red) lpattern(solid)) ///
			xlabel(1 "04" 2 "05" 3 "06" 4 "07" 5 "08" 6 "09" 7 "10" 8 "11" 9 "12" 10 "13" 11 "14" 12 "15" 13 "16" 14 "17", nogrid glcolor(gs14)) ///
			ylabel(, angle(h) nogrid) xtitle("Year") ytitle("Coefficients by year", size(small)) ///
			legend(label(1 "Point Estimates") label(2 "95% Upper bound") label(3 "95% lower bound") size(small)) ///
			graphregion(fcolor(white))
			
		noisily: graph export "$outputs_failure/graphs_matched/event_study_`depvar'.png", replace
		restore
	}
}